﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{

    public class UserDal
    {
        public DapperHelper dapperHelper { get; set; }

        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="systemuserid"></param>
        /// <returns></returns>
        public UserInfo QueryUserById(Guid systemuserid)
        {
            List<UserInfo> userInfos = null;
            SqlBuilder sqlBuilder = new SqlBuilder();
            DynamicParameters sqlParams = new DynamicParameters();

            //获取用户信息
            dapperHelper.ExecueWithoutTransaction(a =>
            {
                string sql = @"select distinct
                                s.domainname,s.FullName,s.new_usertype,s.internalemailaddress as EmailAddress,
                                s.mobilephone,s.systemuserid UserId,s.new_hospital as siteid,
                                h.new_name as [site],b.businessunitid,b.Name as businessunitName,
                                m.new_code as mobilerole_systemuser,h.new_province,h.new_provinceName,
                                h.new_city,h.new_cityName,h.new_area,h.new_areaName
                                from SystemUserBase s
                                left join new_hospital h on h.new_hospitalId = s.new_hospital
                                left join BusinessUnitBase b on b.BusinessUnitId = s.BusinessUnitId
                                left join new_new_mobilerole_systemuserBase ms on ms.systemuserid = s.SystemUserId
                                left join new_mobileroleBase m on m.new_mobileroleId = ms.new_mobileroleid 
                                /**where**/ ";

                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where(" s.systemuserid=@systemuserid and s.IsDisabled =0 and h.statecode = 0 ");
                sqlParams.Add("@systemuserid", systemuserid);

                userInfos = a.Query<UserInfo>(sqlTemplate.RawSql, sqlParams).ToList();
            });

            //处理用户信息
            UserInfo userInfo = userInfos.FirstOrDefault();
            userInfo.mobilerole_systemuser = string.Join("|", userInfos.Select(g => g.mobilerole_systemuser).ToArray());

            return userInfo;
        }


        public UserInfo QueryUserByEmployeeid(string employeeid)
        {
            List<UserInfo> userInfos = null;
            SqlBuilder sqlBuilder = new SqlBuilder();
            DynamicParameters sqlParams = new DynamicParameters();

            //获取用户信息
            dapperHelper.ExecueWithoutTransaction(a =>
            {
                string sql = @"select distinct
                                s.domainname,s.FullName,s.new_usertype,s.internalemailaddress as EmailAddress,
                                s.mobilephone,s.systemuserid UserId,s.new_hospital as siteid,
                                h.new_name as [site],b.businessunitid,b.Name as businessunitName,
                                m.new_code as mobilerole_systemuser,h.new_province,h.new_provinceName,
                                h.new_city,h.new_cityName,h.new_area,h.new_areaName
                                from SystemUserBase s
                                left join new_hospital h on h.new_hospitalId = s.new_hospital
                                left join BusinessUnitBase b on b.BusinessUnitId = s.BusinessUnitId
                                left join new_new_mobilerole_systemuserBase ms on ms.systemuserid = s.SystemUserId
                                left join new_mobileroleBase m on m.new_mobileroleId = ms.new_mobileroleid 
                                /**where**/ ";

                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where(" s.employeeid=@employeeid and s.IsDisabled =0 and h.statecode = 0 ");
                sqlParams.Add("@employeeid", employeeid);

                userInfos = a.Query<UserInfo>(sqlTemplate.RawSql, sqlParams).ToList();
            });

            //处理用户信息
            UserInfo userInfo = userInfos.FirstOrDefault();
            if(userInfos.Select(g => g.mobilerole_systemuser).Count()>0)
            userInfo.mobilerole_systemuser = string.Join("|", userInfos.Select(g => g.mobilerole_systemuser).ToArray());

            return userInfo;
        }


        /// <summary>
        /// 根据用户AD账号获取用户信息
        /// </summary>
        /// <param name="domainName">用户AD账号</param>
        /// <returns></returns>
        public SystemUser QuerySystemUserByDomainName(string domainName)
        {
            string sqlstr = @"select u.SystemUserId,u.domainName,u.fullName,u.employeeId,
                                  h.new_name as hospitalName,
                                  h.new_emedicalhospitalid  
                                  from SystemUserBase as u
                                  left join new_hospitalBase as h 
                                  on u.new_hospital = h.new_hospitalId 
                                  /**where**/ /**orderby**/";

            return dapperHelper.ExecueWithoutTransaction(a =>
            {
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sqlstr);
                sqlBuilder.Where(" u.IsDisabled= 0 ");
                sqlBuilder.Where(" u.DomainName = @domainName ", new { domainName = domainName });
                sqlBuilder.OrderBy(" u.CreatedOn desc ");
                return a.QueryFirstOrDefault<SystemUser>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }
    }
}
